/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.service;

import java.util.List;

import cn.mcsys.myadmin.common.utils.MathUtils;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.service.CrudService;
import cn.mcsys.myadmin.modules.air.entity.AirWorker;
import cn.mcsys.myadmin.modules.air.dao.AirWorkerDao;

/**
 * 工作人员管理Service
 * @author leopold
 * @version 2018-08-13
 */
@Service
@Transactional(readOnly = true)
public class AirWorkerService extends CrudService<AirWorkerDao, AirWorker> {

	@Autowired
	private  AirRidePointService airRidePointService;

	public AirWorker get(Integer id) {
		return super.get(id);
	}
	
	public List<AirWorker> findList(AirWorker airWorker) {
		return super.findList(airWorker);
	}
	
	public Page<AirWorker> findPage(Page<AirWorker> page, AirWorker airWorker) {
		return super.findPage(page, airWorker);
	}
	
	@Transactional(readOnly = false)
	public void save(AirWorker airWorker) {
		if (airWorker.getAirRidePoint() != null){
			AirRidePoint airRidePoint = airRidePointService.get(airWorker.getAirRidePoint().getId());
			if(airWorker.getIsNewRecord()){
				dao.insert(airWorker);
			}else {
				AirRidePoint oldRidePoint = dao.get(airWorker.getId()).getAirRidePoint();
				oldRidePoint.setWorkerSum(MathUtils.reduceOne(oldRidePoint.getWorkerSum()));
				airRidePointService.updateTemplate(oldRidePoint);
				dao.update(airWorker);
			}
			airRidePoint = dao.get(airWorker.getId()).getAirRidePoint();
			airRidePoint.setWorkerSum(MathUtils.addOne(airRidePoint.getWorkerSum()));
			airRidePointService.updateTemplate(airRidePoint);
		}else {
			super.save(airWorker);
		}
	}

	@Transactional(readOnly = false)
	public void delete(AirWorker airWorker) {
		super.delete(airWorker);
	}

    public List<String> findAllPhone() {
		return dao.findAllPhone();
    }
}